package com.sky.controller.admin;
import com.sky.dto.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@Slf4j
@RequestMapping("/admin/order")
@Api(tags = "管理端订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;



    /**
     * 查看订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation(value = "查看订单详情")
    public Result<OrderVO> getOrderDetail(@PathVariable("id") Long id){
        log.info("查看订单详情的参数：订单id为：{}",id);
        OrderVO ordervo = orderService.getOrderDetail(id);
        return Result.success(ordervo);
    }


    /**
     * 订单分页查询
     *
     * @param page
     * @param pageSize
     * @param status   订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @return
     */
//    @GetMapping("/historyOrders")
//    @ApiOperation("订单分页查询")
//    public Result<PageResult> page(int page, int pageSize, Integer status) {
//        log.info("历史订单查询的参数：{}，{}，{}",page,pageSize,status);
//        PageResult pageResult = orderService.pageQueryAllOrders(page,pageSize,status);
//        return Result.success(pageResult);
//    }

    /**
     * 商家拒单
     *
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("商家拒单")
    public Result cancel(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        orderService.adminCancelById(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation(value = "各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics(){
        OrderStatisticsVO orderStatisticsVO = orderService.statisticsOrdersByStatus();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation(value = "派送订单")
    public Result delivery(@PathVariable Long id){
        log.info("派送订单接口的参数：{}",id);
        orderService.delivery(id);
        return Result.success();
    }



    /**
     * 接单
     * @param id
     * @return
     */
    @PutMapping("/confirm")
    @ApiOperation(value = "confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
        log.info("接单的参数：{}",ordersConfirmDTO);
        orderService.confirmOrder(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    @ApiOperation(value = "完成订单")
    public Result complete(@PathVariable Long id){
        log.info("完成订单的参数：{}",id);
        orderService.completeOrder(id);
        return Result.success();
    }

    /**
     * 条件查询
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation(value = "条件查询")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("条件查询的参数：{}",ordersPageQueryDTO);
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 商家 取消订单
     *
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        orderService.AdminCancelById(ordersCancelDTO);
        return Result.success();
    }
}
